'use strict';

const Controller = require('egg').Controller;
const { getCookieValue } = require('../public/processCookies');
/**
* @controller CustomerController 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/
class CustomerController extends Controller {
  async list() {
    /**
        * @summary userlist
        * @description userlist
        * @router get /userlist
        * @request query integer limit
        * @request query integer page
        */
    const params = this.ctx.query;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.customer.list(params);
    this.ctx.body = {
      msg: '',
      status: 200,
      data: result.rows,
      count: result.count,
    };
  }
  async addCustomer() {
    /**
        * @summary addUser
        * @description addUser
        * @router post /addUser
        * @request body addUser
        */
    const params = this.ctx.request.body;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.customer.addCustomer(params);
    global.log.desc = `${getCookieValue(this.ctx.cookies, 'token', 'name')} add Customer`;
    global.log.after = `{name:${params.name},address:${params.address},telephone:${params.telephone}}`;
    this.ctx.body = {
      msg: result.msg,
    };
  }
  async editCustomer() {
    /**
        * @summary editUser
        * @description editUser
        * @router post /editUser
        * @request body editUser
        */
    const params = this.ctx.request.body;
    const { name } = getCookieValue(this.ctx.cookies, 'token', 'user');
    const userResult = await this.ctx.modelmysql.Customer.findOne({ where: { id: params.id } });
    const result = await this.ctx.service.customer.editCustomer(params);
    global.log.desc = `${name} edit Customer`;
    global.log.before = `{name:${userResult.name},address:${userResult.address},telephone:${userResult.telephone}}`;
    global.log.after = `{name:${params.name},address:${params.address},telephone:${params.telephone}}`;
    this.ctx.body = {
      msg: result.msg,
    };
  }
  async deleteCustomer() {
    /**
        * @summary deleteUser
        * @description deleteUser
        * @router get /deleteUser
        * @request query string id
        */
    const params = this.ctx.query;
    const userResult = await this.ctx.modelmysql.Customer.findOne({ where: { id: params.id } });
    if (userResult.onuId) {
      return this.ctx.body = {
        msg: 'The Customer bind Onu',
      };
    }
    const result = await this.ctx.service.customer.deleteCustomer(params);
    const { name } = getCookieValue(this.ctx.cookies, 'token', 'user');
    global.log.desc = `${name} delete Customer`;
    this.ctx.body = {
      msg: result.msg,
    };
  }
  async bindCustomerList() {
    /**
        * @summary bindUserList
        * @description bindUserList
        * @router get /bindUserList
        * @request query integer limit
        * @request query integer page
        */
    const params = this.ctx.query;
    const { id: tid } = getCookieValue(this.ctx.cookies, 'token', 'tenant');
    params.tid = tid;
    const result = await this.ctx.service.customer.bindCustomerList(params);
    this.ctx.body = {
      msg: '',
      status: 200,
      data: result.rows,
      count: result.count,
    };
  }
}

module.exports = CustomerController;
